#include "stdafx.h"
#include <wininet.h>
#pragma comment(lib, "wininet.lib")
#include "WinInternet.h"


CWinInternet::CWinInternet(void)
{
}


CWinInternet::~CWinInternet(void)
{
}


// down load url file
bool CWinInternet::DownLoadFile(const TCHAR * url, const TCHAR * path)
{
	bool bResults = false;
     DWORD byteread=0;  
     char buffer[1024];  
     memset(buffer,0,1024);  
     HINTERNET internetopen;  
   
     internetopen=InternetOpen(_T("File DownLoad"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);  
     if (internetopen==NULL)  
     {   
         return false;  
     }  
   
     HINTERNET internetopenurl;  
     internetopenurl=InternetOpenUrl(internetopen,
		 url,NULL,0,INTERNET_FLAG_RELOAD,0);     
     if (internetopenurl==NULL)  
     {    
         goto there;  
     }  
   
     BOOL hwrite;  
     DWORD written;  
     HANDLE createfile;  
     createfile=CreateFile(path,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);  
     if (createfile==INVALID_HANDLE_VALUE)  
     {    
         goto next;  
     }  
   
     BOOL internetreadfile;  
     while(1)  
     {  
         internetreadfile=InternetReadFile(internetopenurl,buffer,sizeof(buffer),&byteread);  
         if(byteread==0)    
             break;  
         hwrite=WriteFile(createfile,buffer,byteread,&written,NULL);  
         if (hwrite==0)  
         {  
             goto here;  
         }  
		memset(buffer,0,1024);  
     }  
	bResults = true;
 here:  
     CloseHandle(createfile);  
 next:   
     InternetCloseHandle(internetopenurl);  
 there:  
     InternetCloseHandle(internetopen);  
	 return bResults;
}


// get url data
bool CWinInternet::GetUrlData(const TCHAR * url, std::string& content)
{
	bool bResults = false;
     DWORD byteread=0;  
     char buffer[1024];  
     memset(buffer,0,1024);  
     HINTERNET internetopen;  
   

     internetopen=InternetOpen(_T("Get Page Data"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);  
     if (internetopen==NULL)  
     {   
         return false;  
     }  


     HINTERNET internetopenurl;  
     internetopenurl=InternetOpenUrl(internetopen,
		 url,NULL,0,INTERNET_FLAG_RELOAD,0);     
     if (internetopenurl==NULL)  
     {    
         goto there;  
     }  

     BOOL internetreadfile;  
     while(1)  
     {  
		memset(buffer,0,1024);  
         internetreadfile=InternetReadFile(internetopenurl,buffer,1020,&byteread);  
         if(byteread==0)    
             break;  
		 content +=buffer;  
     }  
	bResults = true;

     InternetCloseHandle(internetopenurl);  
 there:  
     InternetCloseHandle(internetopen);  
	 return bResults;
}

// down load url file
bool CWinInternet::HttpsDownLoadFile(const TCHAR * url, const TCHAR * path)
{
	bool bResults = false;
     DWORD byteread=0;  
     char buffer[1024];  
     memset(buffer,0,1024);  
     HINTERNET internetopen;  
   
     internetopen=InternetOpen(_T("File DownLoad"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);  
     if (internetopen==NULL)  
     {   
         return false;  
     }  
   
     HINTERNET internetopenurl;  
     internetopenurl=InternetOpenUrl(internetopen,
		 url,NULL,0,INTERNET_FLAG_IGNORE_CERT_CN_INVALID|INTERNET_FLAG_IGNORE_CERT_DATE_INVALID,0);     
     if (internetopenurl==NULL)  
     {    
         goto there;  
     }  
   
     BOOL hwrite;  
     DWORD written;  
     HANDLE createfile;  
     createfile=CreateFile(path,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);  
     if (createfile==INVALID_HANDLE_VALUE)  
     {    
         goto next;  
     }  
   
     BOOL internetreadfile;  
     while(1)  
     {  
         internetreadfile=InternetReadFile(internetopenurl,buffer,sizeof(buffer),&byteread);  
         if(byteread==0)    
             break;  
         hwrite=WriteFile(createfile,buffer,byteread,&written,NULL);  
         if (hwrite==0)  
         {  
             goto here;  
         }  
		memset(buffer,0,1024);  
     }  
	bResults = true;
 here:  
     CloseHandle(createfile);  
 next:   
     InternetCloseHandle(internetopenurl);  
 there:  
     InternetCloseHandle(internetopen);  
	 return bResults;
}


// get url data
bool CWinInternet::HttpsGetUrlData(const TCHAR * url, std::string& content)
{
	bool bResults = false;
     DWORD byteread=0;  
     char buffer[1024];  
     memset(buffer,0,1024);  
     HINTERNET internetopen;  
   

     internetopen=InternetOpen(_T("Get Page Data"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);  
     if (internetopen==NULL)  
     {   
         return false;  
     }  


     HINTERNET internetopenurl;  
     internetopenurl=InternetOpenUrl(internetopen,
		 url,NULL,0,INTERNET_FLAG_IGNORE_CERT_CN_INVALID|INTERNET_FLAG_IGNORE_CERT_DATE_INVALID,0);     
     if (internetopenurl==NULL)  
     {    
         goto there;  
     }  

     BOOL internetreadfile;  
     while(1)  
     {  
		memset(buffer,0,1024);  
         internetreadfile=InternetReadFile(internetopenurl,buffer,1020,&byteread);  
         if(byteread==0)    
             break;  
		 content +=buffer;  
     }  
	bResults = true;

     InternetCloseHandle(internetopenurl);  
 there:  
     InternetCloseHandle(internetopen);  
	 return bResults;
}
